package com.mxnet.chongjin.supoman;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mxnet.chongjin.supoman.activity.RepairOrderActivity;
import com.mxnet.chongjin.supoman.adapter.MainFragmentAdapter;
import com.mxnet.chongjin.supoman.fragment.MachineFragment;
import com.mxnet.chongjin.supoman.fragment.MachineWorkFragment;
import com.mxnet.chongjin.supoman.fragment.MineFragment;
import com.mxnet.chongjin.supoman.fragment.ServerFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private ViewPager mViewPager;
    private FragmentManager mFragmentManager;
    private FragmentTransaction mFragmentTransaction;
    private MainFragmentAdapter mMainFragmentAdapter;
    private MachineFragment mMachineFragment;
    private ServerFragment mServerFragment;
    private MineFragment mMineFragment;
    private MachineWorkFragment mMachineWorkFragment;
    private List<Fragment> mFragments;
    private LinearLayout mLinearLayout1, mLinearLayout2, mLinearLayout3;
    private ImageView mImageView1, mImageView2, mImageView3;
    private TextView mTextView1, mTextView2, mTextView3;
    private SharedPreferences mPreferences;
    private SharedPreferences.Editor mEditor;
    static public final Locale SWEDISH = new Locale("sv", "Swedish");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mContext = MainActivity.this;
        mPreferences = getSharedPreferences("supoman", MODE_PRIVATE);

        String languageModel=mPreferences.getString("language", Locale.getDefault().getLanguage());
        Configuration mConfiguration=getResources().getConfiguration();
        DisplayMetrics mDisplayMetrics=getResources().getDisplayMetrics();

        if(languageModel.equals("en"))
        {
            mConfiguration.setLocale(Locale.ENGLISH);
            getResources().updateConfiguration(mConfiguration, mDisplayMetrics);
        }
        else if(languageModel.equals("fr"))
        {
            mConfiguration.setLocale(Locale.FRANCE);
            getResources().updateConfiguration(mConfiguration, mDisplayMetrics);
        }
        else if(languageModel.equals("sv"))
        {
            mConfiguration.setLocale(SWEDISH);
            getResources().updateConfiguration(mConfiguration, mDisplayMetrics);
        }
        else if(languageModel.equals("de"))
        {
            mConfiguration.setLocale(Locale.GERMAN);
            getResources().updateConfiguration(mConfiguration, mDisplayMetrics);
        }
        else
        {
            mConfiguration.setLocale(Locale.ENGLISH);
            getResources().updateConfiguration(mConfiguration, mDisplayMetrics);
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        getWindow().setStatusBarColor(Color.TRANSPARENT);

        mEditor = mPreferences.edit();
        mEditor.putString("startPwd", "FFFFFFFF");
        mEditor.commit();


        int useMode = mPreferences.getInt("useMode", 0);
        switch (useMode) {
            case 0:

                break;
            case 1:
                startActivity(new Intent(this, RepairOrderActivity.class));
                this.finish();
                break;
        }

        initData();
        initView();
    }

    public void initData() {
        mFragments = new ArrayList<>();

        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();

        boolean isMatched = mPreferences.getBoolean("isMatched", false);
        boolean matched = getIntent().getBooleanExtra("matched", false);
        if (isMatched) {
            mMachineWorkFragment = new MachineWorkFragment();
            mFragments.add(mMachineWorkFragment);
        } else {
            if (!matched) {
                mMachineFragment = new MachineFragment();
                mFragments.add(mMachineFragment);
            } else {
                mMachineWorkFragment = new MachineWorkFragment();
                mFragments.add(mMachineWorkFragment);
            }
        }

        mServerFragment = new ServerFragment();
        mFragments.add(mServerFragment);

        mMineFragment = new MineFragment();
        mFragments.add(mMineFragment);

        mMainFragmentAdapter = new MainFragmentAdapter(mFragmentManager, mFragments);
    }

    public void initView() {
        mLinearLayout1 = findViewById(R.id.ll_cus_index_1);
        mLinearLayout1.setOnClickListener(this);
        mLinearLayout2 = findViewById(R.id.ll_cus_index_2);
        mLinearLayout2.setOnClickListener(this);
        mLinearLayout3 = findViewById(R.id.ll_cus_index_3);
        mLinearLayout3.setOnClickListener(this);

        mImageView1 = findViewById(R.id.iv_cus_index_1);
        mTextView1 = findViewById(R.id.tv_cus_bottom_1);
        mImageView2 = findViewById(R.id.iv_cus_index_2);
        mTextView2 = findViewById(R.id.tv_cus_bottom_2);
        mImageView3 = findViewById(R.id.iv_cus_index_3);
        mTextView3 = findViewById(R.id.tv_cus_bottom_3);

        mViewPager = findViewById(R.id.vp_main);
        mViewPager.setAdapter(mMainFragmentAdapter);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setCurrentItem(0);
        mImageView1.setSelected(true);
        mTextView1.setSelected(true);

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        getWindow().getDecorView().setSystemUiVisibility(
                                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
                        if (mMachineWorkFragment != null) {
                            //                            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
                        } else {
                            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                        }
                        mLinearLayout1.setSelected(true);
                        mImageView1.setSelected(true);
                        mTextView1.setSelected(true);
                        mLinearLayout2.setSelected(false);
                        mImageView2.setSelected(false);
                        mTextView2.setSelected(false);
                        mLinearLayout3.setSelected(false);
                        mImageView3.setSelected(false);
                        mTextView3.setSelected(false);
                        break;
                    case 1:
                        getWindow().getDecorView().setSystemUiVisibility(
                                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                        mLinearLayout1.setSelected(false);
                        mImageView1.setSelected(false);
                        mTextView1.setSelected(false);
                        mLinearLayout2.setSelected(true);
                        mImageView2.setSelected(true);
                        mTextView2.setSelected(true);
                        mLinearLayout3.setSelected(false);
                        mImageView3.setSelected(false);
                        mTextView3.setSelected(false);
                        break;
                    case 2:
                        getWindow().getDecorView().setSystemUiVisibility(
                                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                        mLinearLayout1.setSelected(false);
                        mImageView1.setSelected(false);
                        mTextView1.setSelected(false);
                        mLinearLayout2.setSelected(false);
                        mImageView2.setSelected(false);
                        mTextView2.setSelected(false);
                        mLinearLayout3.setSelected(true);
                        mImageView3.setSelected(true);
                        mTextView3.setSelected(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_cus_index_1:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.ll_cus_index_2:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.ll_cus_index_3:
                mViewPager.setCurrentItem(2);
                break;
        }
    }

    private long firstTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 2000) {
                Toast.makeText(MainActivity.this, getResources().getString(R.string.quit), Toast.LENGTH_SHORT).show();
                firstTime = secondTime;
                return true;
            } else {
                System.exit(0);
            }
        }
        return true;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mMachineWorkFragment != null) {
            mFragments.remove(mMachineWorkFragment);
            mMachineWorkFragment = new MachineWorkFragment();
            mFragments.add(mMachineWorkFragment);
            mMainFragmentAdapter = new MainFragmentAdapter(mFragmentManager, mFragments);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 20) {
            mMineFragment.onCreate(null);
            mViewPager.setCurrentItem(2);
        }
    }

    private void restartApplication() {
        final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }
}
